﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ipaidabribe.Code;

namespace ipaidabribe.AdminCP
{
    public partial class edit : System.Web.UI.Page
    {
        public string _option = "";
        public int _id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LOGIN"] != null)
            {
                if (Session["LOGIN"].ToString() != "1")
                    Response.Redirect("Login.aspx");
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            if (Request.QueryString["id"] != null)
            {
                _id = Convert.ToInt32(Request.QueryString["id"].ToString());
            }
            if (Request.QueryString["option"] != null)
            {
                _option = Request.QueryString["option"].ToString();
                MakeAble(_id);
                Response.Redirect("index.aspx");
            }

            if (!Page.IsPostBack)
            {
                //bind city
                var db = new ipaidabribeEntities();
                var city = db.Cities.OrderBy("it.Order,it.Name").ToList();
                ddlCity.DataTextField = "Name";
                ddlCity.DataValueField = "Name";
                ddlCity.DataSource = city;
                ddlCity.DataBind();
                //chon  bo, nganh
                var department = db.Departments.Where(t => t.Enable != false).ToList();
                department.Insert(0, new Department() { Id = 0, Name = "Chọn" });
                ddlDepartment.DataTextField = "Name";
                ddlDepartment.DataValueField = "Name";
                ddlDepartment.DataSource = department;
                ddlDepartment.DataBind();

                //Load tin
                var bribe = db.Bribes.Where(t => t.Id == _id).FirstOrDefault();
                if (bribe != null)
                {
                    //var cityBribe = db.Cities.Where(t => t.Name == bribe.City).FirstOrDefault();
                    ddlCity.SelectedValue = bribe.City;
                    ddlDepartment.SelectedValue = bribe.Departments;
                    txt_amt_paid.Text = bribe.AmountPaid;
                    txt_date_paid.Text = bribe.DateReport.ToString();
                    txt_other_location.Text = bribe.OfficeLocation;
                    ddlBribe_type.SelectedValue = bribe.BribeType;
                    txt_name.Text = HttpUtility.HtmlDecode(bribe.Title.Trim());
                    txt_addi_info.InnerText = HttpUtility.HtmlDecode(bribe.Content.Trim());
                }
            }
            
        }

        private void MakeAble(int _id)
        {
            var context = new ipaidabribeEntities();
            var bribe = context.Bribes.Where(t => t.Id == _id).FirstOrDefault();
            if (bribe != null)
            {
                if (bribe.Enable == true)
                    bribe.Enable = false;
                else
                    bribe.Enable = true;

                context.SaveChanges();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //update
            var city = ddlCity.SelectedItem.Text.Trim();
            if (ddlDepartment.SelectedIndex < 1)
            {
                lblError.Text = "Chưa chọn Bộ phận!!!";
                lblError.Visible = true;
                return;
            }
            var department = ddlDepartment.SelectedItem.Text.Trim();

            var money = txt_amt_paid.Text.Trim();
            if (txt_date_paid.Text.Trim() == "")
            {
                lblError.Text = "Chưa chọn Ngày!!!";
                lblError.Visible = true;
                return;
            }
            var date = DateTime.Now;
            try
            {
                date = Convert.ToDateTime(txt_date_paid.Text.Trim().Replace("-", "/"));
            }
            catch (Exception ex)
            {
                //throw;
            }

            var location = txt_other_location.Text.Trim();
            var type = ddlBribe_type.SelectedValue.Trim();
            var title = txt_name.Text.Trim();
            var info = txt_addi_info.Value.Trim();
            
            //kiem tra
            if (money == "")
            {
                lblError.Text = "Chưa nhập Số tiền đưa!!!";
                lblError.Visible = true;
                return;
            }
            if (txt_date_paid.Text.Trim() == "")
            {
                lblError.Text = "Chưa nhập Ngày đưa hối lộ!!!";
                lblError.Visible = true;
                return;
            }
         
            if (title == "")
            {
                lblError.Text = "Chưa nhập Tiêu đề!!!";
                lblError.Visible = true;
                return;
            }
            if (info == "")
            {
                lblError.Text = "Chưa nhập Nội dung!!!";
                lblError.Visible = true;
                return;
            }
            
            var db = new ipaidabribeEntities();
            var bribe = db.Bribes.Where(t => t.Id == _id).FirstOrDefault();
            
            bribe.Cat = 0;
            bribe.City = city;
            bribe.Departments = department;
            bribe.Transaction = "";
            bribe.AmountPaid = money;
            bribe.DatePaid = date;
            bribe.OfficeLocation = location;
            bribe.BribeType = type;
            bribe.Title = HttpUtility.HtmlEncode(title);
            bribe.Content = HttpUtility.HtmlEncode(info);
            bribe.DateReport = DateTime.Now;
            bribe.TotalComment = 0;
            bribe.Enable = true;
            
            var result = db.SaveChanges();
            //db.AddToBribes(bribe);
            if (result > 0)
            {
                lblError.Text = "Thành công!!!";
                lblError.Visible = true;
               
            }
        }
    }
}